package com.atguigu.guli.serviceedu.controller;


import com.atguigu.guli.serviceedu.entity.Teacher;
import com.atguigu.guli.serviceedu.entity.add.TeacherAdd;
import com.atguigu.guli.serviceedu.entity.query.TeacherQuery;
import com.atguigu.guli.serviceedu.entity.update.TeacherUpdate;
import com.atguigu.guli.serviceedu.service.TeacherService;
import com.atguigu.servicebase.result.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-07-28
 */
@Api(tags = "教师管理")
@CrossOrigin
@RestController
@RequestMapping("/admin/edu/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;


    @ApiOperation("获取讲师列表")
    @GetMapping
    public R list(){
//        Assert.notNull(null, ResponseEnum.ERROR);
        List<Teacher> list = teacherService.list(null);
        return R.ok().data("items", list);
    }

    @CacheEvict(value = "teacher-index", allEntries=true)
    @ApiOperation("根据ID删除讲师")
    @DeleteMapping("{id}")
    public R removeById(
            @ApiParam(name = "id", value = "讲师ID", required = true, example = "1")
            @PathVariable String id){
        boolean result = teacherService.removeById(id);
        return R.ok();
    }

    @ApiOperation(value = "分页讲师列表")
    @GetMapping("{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true, example = "1")
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true, example = "5")
            @PathVariable Long limit,
            @ApiParam(name = "teacherQuery", value = "查询对象", required = false, example = "null")
                    TeacherQuery teacherQuery){
        Page<Teacher> pageParam = new Page<>(page, limit);
        teacherService.pageQuery(pageParam, teacherQuery);
        List<Teacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return  R.ok().data("total", total).data("rows", records);
    }

    @CacheEvict(value = "teacher-index", allEntries=true)
    @ApiOperation(value = "新增讲师")
    @PostMapping
    public R save(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody TeacherAdd teacherAdd){
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherAdd,teacher);
        teacherService.save(teacher);
        return R.ok();
    }

    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("{id}")
    public R getById(
            @ApiParam(name = "id", value = "讲师ID", required = true, example = "1")
            @PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        return R.ok().data("item", teacher);
    }

    @ApiOperation(value = "根据ID修改讲师")
    @PutMapping("{id}")
    public R updateById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id,
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody TeacherUpdate teacherUpdate){
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherUpdate,teacher);
        teacher.setId(id);
        teacherService.updateById(teacher);
        return R.ok();
    }
}

